package com.xinqi.modules.sys.dict.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.sys.dict.convert.DictVersionStageConvert;
import com.xinqi.modules.sys.dict.domain.DictVersionStageEntity;
import com.xinqi.modules.sys.dict.dto.req.DictVersionStageCreateDTO;
import com.xinqi.modules.sys.dict.dto.req.DictVersionStageDeleteDTO;
import com.xinqi.modules.sys.dict.dto.req.DictVersionStageQueryDTO;
import com.xinqi.modules.sys.dict.dto.req.DictVersionStageUpdateDTO;
import com.xinqi.modules.sys.dict.dto.rsp.DictVersionStageResultDTO;
import com.xinqi.modules.sys.dict.manager.DictVersionStageManager;
import com.xinqi.modules.sys.dict.service.DictVersionStageService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import javax.validation.Valid;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * 字典-版本学科服务层实现
 *
 * @author: linkai
 * @date: 2023/05/07
 */
@Validated
@Service("dictVersionStageService")
@RequiredArgsConstructor
public class DictVersionStageServiceImpl implements DictVersionStageService {
    private final DictVersionStageConvert dictVersionStageConvert;
    private final DictVersionStageManager dictVersionStageManager;

    /**
     * 分页查询 字典-版本学科数据
     */
    @Override
    @DS("slaver")
    public PageInfo<DictVersionStageResultDTO> page(Param pageable, DictVersionStageQueryDTO query) {
        IPage<DictVersionStageEntity> page = dictVersionStageManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, dictVersionStageConvert::convert);
    }

    /**
     * 查询 字典-版本学科数据
     */
    @Override
    public List<DictVersionStageResultDTO> find(DictVersionStageQueryDTO query) {
        return dictVersionStageManager.list(wrapper(query)).stream()
            .map(dictVersionStageConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<DictVersionStageEntity> wrapper(DictVersionStageQueryDTO query) {
        LambdaQueryWrapper<DictVersionStageEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(DictVersionStageEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 字典-版本学科数据
     */
    @Override
    public DictVersionStageResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(dictVersionStageManager.findById(id)).map(dictVersionStageConvert::convert).orElse(null);
    }

    /**
     * 新增 字典-版本学科数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(@Valid DictVersionStageCreateDTO dto) {
        DictVersionStageEntity entity = dictVersionStageConvert.create(dto);
        dictVersionStageManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 字典-版本学科数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(@Valid DictVersionStageUpdateDTO dto) {
        Optional.ofNullable(dictVersionStageManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        DictVersionStageEntity entity = dictVersionStageConvert.update(dto);
        return dictVersionStageManager.updateById(entity);
    }

    /**
     * 根据id删除 字典-版本学科数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return dictVersionStageManager.removeById(id);
    }

    /**
     * 根据id删除 字典-版本学科数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(DictVersionStageDeleteDTO dto) {
        return deleteById(dto.getId());
    }

}
